<?php
namespace Home\Controller;
/**
 * Created by PhpStorm.
 * User: yhg97p
 * Date: 2016/4/6
 * Time: 21:14
 */
class CrontabController extends \Think\Controller {

    /**
     *
     */
    public function index(){
        /**
         *
         */
    }

    /**
     * 产品到期提醒
     */
    public function investexpiration(){
        //获取所有正在出售的基金
        $fundinfo = M()->query("SELECT a.`user_id`,b.fund_name,FROM_UNIXTIME(a.create_time) as c_time FROM mt_invest a
                    LEFT JOIN mt_fund b ON a.`fund_id`=b.`id`
                    WHERE b.`status`=1 AND a.`status`=1 and a.paysuccess!=4 AND TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(a.create_time))=b.`std_time`-1");
        if(count($fundinfo)){
            foreach($fundinfo as $key=>$val){
                $userids = array();
                $userids[] = $val['user_id'];
                $fund_name = $val['fund_name'];
                $c_time = $val['c_time'];
                $content = array("name"=>"产品到期通知","content"=>"你于".$c_time."购买的产品".$fund_name."明天到期");
                sendjpushbyuser($userids,$content);
            } 
        }
    }
    /**
     * 系统赠送的基金
     */
    public function activeinvest(){
        //获取购买最多的基金
        $fundinfo = M()->query("SELECT id,fund_name FROM mt_invest WHERE STATUS='1' GROUP BY fund_id ORDER BY COUNT(id) DESC LIMIT 1");
        $fund_name = $fundinfo['fund_name'];

        //获取体验金
        $fundinfo = M()->query("SELECT a.`user_id`,b.fund_name,FROM_UNIXTIME(a.create_time) as c_time FROM mt_invest a LEFT JOIN mt_fund b ON a.`fund_id`=b.`id` WHERE b.`status`=1 AND a.`status`=1 AND a.paysuccess='4' AND TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(a.create_time))=4");

        if(count($fundinfo)){
            foreach($fundinfo as $key=>$val){
                $userids = array();
                $userids[] = $val['user_id'];
                $fund_name = $val['fund_name'];
                $c_time = $val['c_time'];
                $content = array("name"=>"体验金到期通知","content"=>"系统于".$c_time."赠送的体验金三天之后到期，继续投资您可以购买热门主题：".$fund_name."，更多主题等您选择");
                sendjpushbyuser($userids,$content);
            }
        }
    }
    /**
     * 删除头一天未支付的订单
     */
    public function deletenopayorder(){
        $time = strtotime(date("Y-m-d 00:00:00"));
        //获取体验金
        $where['paysuccess']='0';
        $where['create_time'] =  array("lt",$time);

        M("Invest")->where($where)->delete();


    }
}